package com.property.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.print.CancelablePrintJob;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;








import com.property.biz.CommunityBiz;
import com.property.biz.LrrelationBiz;
import com.property.biz.ManagementBiz;
import com.property.biz.OwnerBiz;
import com.property.biz.UserBiz;
import com.property.entity.Community;
import com.property.entity.Lrrelation;
import com.property.entity.Management;
import com.property.entity.Owner;
import com.property.entity.Role;
import com.property.entity.User;

@Controller
//@RequestMapping("")
public class LoginController {
	
	@Autowired
	private UserBiz userBiz;
	@Autowired
    private  OwnerBiz  ownerbiz;
	@Autowired 
	private CommunityBiz communitybiz;
	@Autowired
	private ManagementBiz managementBiz;
	@Autowired
	private LrrelationBiz lrrelationBiz;
	
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	@RequestMapping("/1031")
	public String qx(){
		return "1031";
	}
	@RequestMapping("/2041")
	public String s(){
		return "2041";
	}
	@RequestMapping(value="/UserLogin",method=RequestMethod.POST)
	public String UserLogin(Model model,String name,String password,HttpSession session){
		User user=userBiz.loginUser(name, password);
		session.setAttribute("error", "");
		if(user!=null){
			SimpleDateFormat fmt=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			userBiz.upLoginTime(user.getId(), fmt.format(new Date()));//更新最新登陆时间
			
			session.setAttribute("user", user);
			Management management=managementBiz.ManGet(user.getId());
		
			session.setAttribute("management", management);
			if(user.getRole().getId()==1){
				
				return "redirect:/owner/index";
			}else if(user.getRole().getId()==2){
				List<Lrrelation>  lrrelations = lrrelationBiz.getLimit(management.getId());
				session.setAttribute("lrrelations", lrrelations);
				return "redirect:/main/index";
			}else if(user.getRole().getId()==3){
				return "redirect:/admin/index";
			}else{
				return "login";
			}
		}else{
			session.setAttribute("error", "用户名或密码错误");
			return "login";
		}
	}
	@RequestMapping("/cancel")
	public String cancel(HttpSession session){
		session.removeAttribute("user");
		session.removeAttribute("url");
		return "login";
	}
	
	@RequestMapping("/register")	
	public String register(Model model) {	
		//List<Community> communities=communitybiz.getAll();
		//model.addAttribute("communities", communities);			
		return "register";
	}
	
	@RequestMapping(value="/doregister",method=RequestMethod.POST)
	public String doregister(String username,String pass,String email) {	
		User user=new User();
		Role role=new Role();
		role.setId(1);
		user.setUserName(username);
		user.setPassWord(pass);
		user.setRole(role);
		user.setEmail(email);
		user.setModifyDate(new Date());
		
 	    userBiz.addUser(user);
		return "login";
	}
	
	@RequestMapping("/check")
	public void check(String username,HttpServletResponse response) throws IOException {
		int count=userBiz.checkUsername(username);
		PrintWriter out=response.getWriter();
		out.print(count);
	}
	
	
	
	
}
